const fs = require('fs');
const { remote  } = require('electron')
const app = remote.app
const path  = require('path');
//set windows proxy
export function getProxyConfig(isStart){
    let text = "@echo off\n";
    text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f\n';
    text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyServer /d "" /f\n';
    text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyOverride /t REG_SZ /d 0 /f\n';
    if(isStart){
        text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f\n';
        text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyServer /d "127.0.0.1:7896" /f\n';
        text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyOverride /t REG_SZ /d "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;127.0.0.1" /f\n';
    }else{
        text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f\n';
        text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyServer /d "" /f\n';
        text +='reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyOverride /t REG_SZ /d "" /f\n';
    }
    return text;
}

//set user home config
export function getUserHomeConfig(remotePort){
    let text = "mixed-port: 7896\n";
    text += "allow-lan: false\n";
    text += "log-level: silent\n";
    text += "external-controller: 127.0.0.1:" + remotePort +"\n";
    text += "secret: \'\'\n";
    text += "ipv6: false";
    return text;
}

export function writeConfigToPath(text,fileName){
    let rootUrl = app.getPath("home").replace(/\\/g,"/");
    rootUrl += '/.config/clash';
    const configUrl = path.posix.join(rootUrl,fileName);
    fs.writeFile(configUrl, text, function (err) {
        if (err) console.error(err);
        // console.log('数据写入成功');
    });
    return configUrl;
}
// random generate num
export function getRandom(min,max) { //根据最小值和最大值产生一个随机数
    return Math.floor(Math.random() * (max - min) + min);
}
export function getCoreExePath(){
    let coreExePath;
    if (process.env.NODE_ENV === 'development') {
        coreExePath = path.join(process.cwd(), '/extraResources/clash.exe')
    }else{
        coreExePath = path.join(process.cwd(), '/resources/extraResources/clash.exe')
    }
    return coreExePath;
}
export function getConfigPath(){
    let configPath;
    if (process.env.NODE_ENV === 'development') {
        configPath = path.join(process.cwd(), '/extraResources/config.yml')
    }else{
        configPath = path.join(process.cwd(), '/resources/extraResources/config.yml')
    }
    return configPath;
}
export function formatTimestampToDate(timestamp) {
    if (timestamp === 0 || timestamp == null) {
        return ''
    } else {
        var date = new Date(timestamp * 1000)
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        var D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' '
        var H = date.getHours() + ':'
        var M2 = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        var S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        return Y + M + D + H + M2 + S
    }
}

export default {
    getProxyConfig,
    getUserHomeConfig,
    writeConfigToPath,
    getCoreExePath,
    getConfigPath,
    formatTimestampToDate
}